#region Imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using Workflow.NET;
using WorFlowInterface = Workflow.NET.Engine.Interfaces;
using WorkFlowAction = Workflow.NET.ActionHandler;
using WorkFlowEngine = Workflow.NET.Engine;
using PHSRAG.Insight21.Policy;
using System.Threading;
using PHSRAG.Utility;
using System.Collections;
using System.IO;
using System.Text;
using SkeltaEngine = Workflow.NET.Engine;
#endregion

namespace PHSRAG.Insight21.InsightWorkFlow
{
    #region Class RemoveInactiveUsers
    public class RemoveInactiveUsers:Workflow.NET.Engine.Interfaces.IActionRun 
    {
        public RemoveInactiveUsers()
		{			
		}

        public WorFlowInterface.ActionResult Run (int executionId, int executionDetailsId, WorkFlowEngine.Context currentContext, Action currentAction, string inlinkOutput, bool retry, out string output)
        {
            //System.Diagnostics.Debugger.Launch();
            output="Success";	
            Log logger = currentContext.log;
            HumansWorkFlow humansWF = new HumansWorkFlow(new NullCachePolicy());

            SkeltaEngine.Context workflowContext = null;
            workflowContext = humansWF.GetWorkFlowContext(Int32.Parse(currentContext.Contents["ppid"].Value.ToString()));

            object insightApplicationName = currentContext.Contents["insightApplicationName"];
            HumansWorkFlow.InsightApplicationName = (null == insightApplicationName) ? string.Empty : ((Content)insightApplicationName).Value.ToString();

            Thread.SetData(Thread.GetNamedDataSlot("Connections"), HumansWorkFlow.Connections);
            SqlConnection connection = new SqlConnection(HumansWorkFlow.InsightConnection);
                                    
            try
            {
                ArrayList studystaffList = new ArrayList();
                StringBuilder personIds = new StringBuilder();

                string actionType = (string)((Workflow.NET.Property)currentAction.Properties["ActionType"]).Value;
                studystaffList = workflowContext.Variables[actionType].Value as ArrayList;                

                if (studystaffList != null && studystaffList.Count != 0)
                {
                    for (int i = 0; i < studystaffList.Count; i++)
                    {
                        personIds.Append(studystaffList[i].ToString());
                    }
                }

                connection.Open();
                DataTable checkVetoEmails = DataAccess.GetDataTable(connection,
                                                                    "CheckWorkflowEmailAccounts",
                                                                     DataAccess.BuildSqlParameterArray("@admPersonId", SqlDbType.VarChar, personIds.ToString()));

                if (checkVetoEmails != null && checkVetoEmails.Rows.Count > 0)
                {
                    int itemCount = checkVetoEmails.Rows.Count;
                    studystaffList.Clear();

                    for (int i = 0; i <= itemCount-1; i++)
                    {
                        studystaffList.Add(checkVetoEmails.Rows[i].ItemArray[0].ToString());
                    }

                    humansWF.SetContextVariables(actionType, studystaffList, Int32.Parse(currentContext.Contents["ppid"].Value.ToString()));
                }                
            }
            catch (Exception ex)
            {
                logger.LogMessage(ReasonConst.Error, "Unable to  " + ex.ToString());
                output = "Failure";
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                    connection.Close();
                humansWF.Dispose();
            }
            return WorFlowInterface.ActionResult.Completed; 
        }
        public virtual Workflow.NET.Interfaces.IActivityWebUIHandler GetActivityWebUIHandler()
        {
            return null;
        } 
    }
    
    #endregion
}
